<?php
namespace Admin\Controller;
use Think\Controller;
class IndexController extends Controller {
    //首页显示
    public function index(){
        $this->display();
    }
    //大师列表
    public function masterlist(){
        $master=M('master')->select();
        $num = M('master')->count();
        $this->num =$num;
        $this->assign('master',$master);
        $this->display();
    }
    //大师课程列表
    public function masterclasslist(){
        $class=M('class')->select();
        $num = M('class')->count();
        $this->num =$num;
        $this->assign('class',$class);
        $this->display();
    }
    //大师问答列表
     public function questionlist(){
        $questions=M('questions')->select();
        $num = M('questions')->count();
        $this->num =$num;
        $this->assign('questions',$questions);
        $this->display();
    }
    //添加大师
     function addmaster(){
        $mname=$_POST['mname'];
        $mintro=$_POST['mintro'];
        $mpic=$_POST['mpic'];    
        $dashi=D('master');
        if ($dashi->create()) 
        {
            if (false !== $dashi->add()) {
                //echo $user->getLastSql();
                $this->success('添加成功！', 'masterlist');
            }
            else
            {
                $this->error('添加失败，数据错误','masterlist');
            }
        }
            $this->display();
        }
      //修改大师页面显示
        function mastermod(){
            $mid = $_GET['id'];
            $re = M('master')->where('mid='.$mid)->find();
            $this->re = $re;
            $this->display();
        }
        //修改大师
        function mastermod1(){
            //$class = M("class");
            $mid = $_POST['mid'];
            $data['mname']=$_POST['mname'];
            $data['mintro'] = $_POST['mintro'];
            $data['mpic']=$_POST['mpic'];
            $int = M('master')->where('mid='.$mid)->save($data);
            if($int){
                $this->success('修改成功！','masterlist');
            }
            else{
                echo "$data";
                $this->error('修改失败！','masterlist');
            }
    
        }
        //删除大师
        function delmaster(){
        $mid=I('get.id');
        $data=M('master')->where(array('mid'=>$mid))->find();
        if($data){
            
                $re=M('master')->where(array('mid'=>$mid))->delete();
            if($re)
                $this->success('删除成功！','masterlist');
            else
                $this->error('删除失败！','masterlist');
        }else
            $this->error('该大师信息意外丢失，删除失败！');
        }
//添加大师课程
        function addclass(){
        $cname=$_POST['cname'];
        $cintro=$_POST['cintro'];
        $mname=$_POST['mname'];
        $cvideo=$_POST['cvideo'];
        $dclass=D('class');     
        if ($dclass->create()) 
        {
            if (false !== $dclass->add()) {
                //echo $user->getLastSql();
                $this->success('添加成功！', 'classclasslist');
            }
            else
            {
                $this->error('添加失败，数据错误','classclasslist');
            }
        }
            $this->display();
        }
//修改大师课程页面显示
        function dclassmod(){
            $cid = $_GET['id'];
            $re = M('class')->where('cid='.$cid)->find();
            $this->re = $re;
            $this->display();
        }
        //修改大师课程
        function dclassmod1(){
            //$class = M("class");
            $cid = $_POST['cid'];
            $data['cname']=$_POST['cname'];
            $data['mname']=$_POST['mname'];
            $data['cintro'] = $_POST['cintro'];
            $data['cvideo']=$_POST['cvideo'];
            $int = M('class')->where('cid='.$cid)->save($data);
            if($int){
                $this->success('修改成功！','classclasslist');
            }
            else{
                
                $this->error('修改失败！','classclasslist');
            }
    
        }
//删除大师课程
        function delmasterclass(){
        $cid=I('get.id');
        $data=M('class')->where(array('cid'=>$cid))->find();
        if($data){
            
                $re=M('class')->where(array('cid'=>$cid))->delete();
            if($re)
                $this->success('删除成功！','masterclasslist');
            else
                $this->error('删除失败！','masterclasslist');
        }else
            $this->error('该课程信息意外丢失，删除失败！');
        }
        //删除大师问答
        function delquestion(){
        $qid=I('get.id');
        $data=M('questions')->where(array('qid'=>$qid))->find();
        if($data){
            
                $re=M('questions')->where(array('qid'=>$qid))->delete();
            if($re)
                $this->success('删除成功！','questionlist');
            else
                $this->error('删除失败！','questionlist');
        }else
            $this->error('该问题信息意外丢失，删除失败！');
        }
       //搜索大师
        public function search(){
        $key = $_GET['key'];
        $masternum = M('master')->where('mname like "%'.$key.'%"')->count();
        $Page = new \Think\Page($masternum,2);
        $this->masternum =$masternum;
        $this->masterpage=$Page->show();
        if (masternum) {
        $this->masterlist=M('master')
            ->where('mname like "%'.$key.'%"')
            ->order('mid')
            ->limit($Page->firstRow,2)
            ->select();
        
        $this->display();
        }
        else $this->error('没有相关信息！');
        }
        //搜索大师课程
      public function searchclass(){
        $key = $_GET['key'];
        $classnum = M('class')->where('cname like "%'.$key.'%"')->count();
        $Page = new \Think\Page($masternum,2);
        if ($classnum) {
        $this->classnum =$classnum;
        $this->classpage=$Page->show();
        $this->class=M('class')
            ->where('cname like "%'.$key.'%"')
            ->order('cid')
            ->limit($Page->firstRow,2)
            ->select();
        $this->display();
        }
        else $this->error('没有相关信息！');
        }
function userList(){
        $userlist=M('userlist')->select();
        $this->assign('userlist',$userlist);
        $this->display();
    }


}
